package com.chen.recruit.controller;

import java.util.Arrays;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chen.recruit.common.annotation.Log;
import com.chen.recruit.common.api.CommonResult;
import com.chen.recruit.common.enums.BusinessType;
import com.chen.recruit.common.utils.ExcelUtils;
import com.chen.recruit.domain.OperateLog;
import com.chen.recruit.domain.model.PageQuery;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.chen.recruit.domain.vo.OperateLogVo;
import com.chen.recruit.service.OperateLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "操作日志管理 OperateLogController")
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/operate-log")
public class OperateLogController {

    private final OperateLogService operateLogService;

    @ApiOperation("删除操作日志")
    @SaCheckPermission("system:operate-log:delete")
    @Log(module = "系统管理-操作日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public CommonResult delete(@PathVariable Integer[] ids) {
        return operateLogService.removeByIds(Arrays.asList(ids)) ? CommonResult.success() : CommonResult.fail();
    }

    @ApiOperation("查询操作日志列表")
    @SaCheckPermission("system:operate-log:list")
    @GetMapping("/list")
    public CommonResult list(OperateLogVo operateLogVo, PageQuery pageQuery) {
        return CommonResult.success(operateLogService.page(pageQuery.build(), buildQueryWrapper(operateLogVo)));
    }

    @ApiOperation("导出操作日志列表")
    @SaCheckPermission("system:operate-log:export")
    @Log(module = "系统管理-操作日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OperateLogVo vo) {
        ExcelUtils.exportExcel(response, "操作日志记录", OperateLog.class, operateLogService.list(buildQueryWrapper(vo)));
    }

    private LambdaQueryWrapper<OperateLog> buildQueryWrapper(OperateLogVo vo) {
        return Wrappers.<OperateLog>lambdaQuery()
                .like(StringUtils.isNotBlank(vo.getModule()), OperateLog::getModule, vo.getModule())
                .like(StringUtils.isNotBlank(vo.getOperator()), OperateLog::getOperator, vo.getOperator())
                .eq(ObjectUtil.isNotNull(vo.getStatus()), OperateLog::getStatus, vo.getStatus())
                .eq(StringUtils.isNotBlank(vo.getReqMethod()), OperateLog::getReqMethod, vo.getReqMethod())
                .eq(ObjectUtil.isNotNull(vo.getBusinessType()), OperateLog::getBusinessType, vo.getBusinessType())
                .ge(StringUtils.isNotBlank(vo.getBeginTime()), OperateLog::getOperateTime, vo.getBeginTime())
                .le(StringUtils.isNotBlank(vo.getEndTime()), OperateLog::getOperateTime, vo.getEndTime());
    }

}
